﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using Empresa.Sistema.Datos.Entidades;

namespace Empresa.Sistema.Datos
{
    public class Cargos
    {

        private string ConnectionString = "Data Source=.;Initial Catalog=SistemaBibliotecaV1;Integrated Security=True";
        public void Adicionar(Cargo aut)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_InsertarCargo";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Codigo", aut.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", aut.Descripcion));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Modificar(Cargo aut)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ModificarCargo";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", aut.Id));
                        cmd.Parameters.Add(new SqlParameter("@Codigo", aut.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", aut.Descripcion));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Eliminar(Cargo aut)
        {

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_EliminarCargo";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", aut.Id));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public List<Cargo> Listar()
        {
            List<Cargo> lista = new List<Cargo>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ListarCargo";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var car = new Cargo();
                            car.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            car.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            car.Descripcion = rdr.GetString(rdr.GetOrdinal("Descripcion"));

                            lista.Add(car);
                        }
                    }

                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }

        public List<Cargo> ConsultaPorDescripcion(string Descripcion)
        {
            List<Cargo> lista = new List<Cargo>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ConsultaCargoPorDescripcion";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", Descripcion));
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var car = new Cargo();
                            car.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            car.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            car.Descripcion = rdr.GetString(rdr.GetOrdinal("Descripcion"));
                            lista.Add(car);
                        }
                    }
                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }


    }
}
